define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            var ids = Fast.api.query('ids');
            Table.api.init({
                extend: {
                    index_url: `order/product/index?ids=${ids}`,
                    add_url: 'order/product/add',
                    edit_url: 'order/product/edit',
                    del_url: 'order/product/del',
                    multi_url: 'order/product/multi',
                    import_url: 'order/product/import',
                    deliver_url: 'order/product/deliver',
                    table: 'order_product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order.code', title: __('Orderid')},
                        {field: 'business.nickname', title: __('Busid')},
                        {field: 'pronum', title: __('Pronum')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'total', title: __('Total'), operate:'BETWEEN'},
                        {field: 'rate', title: __('Rate'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'comtime', title: __('Comtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'query_num', title: __('Query_num')},
                        {field: 'query_code', title: __('Query_code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'query_time', title: __('Query_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'query_qrcord', title: __('Query_qrcord'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {'0' : __('Unfilled'), '1' : __('Delivered'), '2' : __('Received'), '3' : __('Completed'), '-1' : __('Refund only'), '-2' : __('Refund and return'), '-3' : __('After sale'), '-4' : __('Success'), '-5' : __('Failure')}, formatter: Table.api.formatter.status},
                        {field: 'expressid', title: __('Expressid')},
                        {field: 'expresscode', title: __('Expresscode'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'adminid', title: __('Adminid')},
                        {field: 'checkmanid', title: __('Checkmanid')},
                        {field: 'shipmanid', title: __('Shipmanid')},
                        {field: 'examinereason', title: __('Examinereason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'refundreason', title: __('Refundreason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                            {
                                name: 'deliver',
                                title: '发货',
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                classname: "btn btn-xs btn-success btn-dialog",
                                icon: 'fa fa-truck',
                                url: $.fn.bootstrapTable.defaults.extend.deliver_url,
                                visible: function (row) {
                                    return row.status == 0;
                                }
                            },
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/product/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/product/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/product/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        deliver: function(){
            $('#notrandom').click(function(){
                $('#expresscode').removeAttr('readonly')
            })
            $('#random').click(function(){
                $('#expresscode').attr('readonly','readonly')
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
